﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Configuration;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace ReplayHouse
{
    public partial class Form1 : Form
    {
        WebRequestBusiness request = new WebRequestBusiness();
        UrlListBusiness urlRequest = new UrlListBusiness();
        public int SetTime { get; set; }

        public Form1()
        {
            InitializeComponent();
            InitData();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            var settings = ReplyHouseConst.SettingList;
            for (int i = 0; i < settings.Count; i++)
            {
                this.lbxUrlList.Items.Add(settings[i]);
            }

            this.txtUserName.Text = ReplyHouseConst.UserName;
            this.txtPassword.Text = ReplyHouseConst.UserPassword;
        }


        //登录
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            request.Login();
        }


        //启动
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                return;
            }
            backgroundWorker1.RunWorkerAsync(this.txtInput.Text);

            int setTime = 0;
            if (int.TryParse(this.txtTime.Text, out setTime))
                this.SetTime = setTime;

            timer1.Interval = this.SetTime;
        }


        //添加URL 
        private void btnAddUrl_Click(object sender, EventArgs e)
        {
            string inputUrl = this.txtInputUrl.Text.Trim();
            this.lbxUrlList.Items.Add(inputUrl);
        }

        //移除URL
        private void btnRemove_Click(object sender, EventArgs e)
        {
            object removeUrl = this.lbxUrlList.SelectedItem;
            this.lbxUrlList.Items.Remove(removeUrl);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;
            List<string> urlList = new List<string>();
            string inputText = e.Argument.ToString();
            foreach (var item in this.lbxUrlList.Items)
            {
                urlList.Add(item.ToString());
            }
            urlRequest.PostMessage(urlList, inputText, worker);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.txtShow.Text += e.UserState.ToString() + Environment.NewLine;
        }


        /// <summary>
        /// 定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                return;
            }
            backgroundWorker1.RunWorkerAsync(this.txtInput.Text);
        }
    }
}
